package com.hengxinguotong.zhihuichengjian.ui.engineering.task;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.RelativeLayout;

import com.google.gson.Gson;
import com.hengxinguotong.zhihuichengjian.R;
import com.hengxinguotong.zhihuichengjian.adapter.StructureTreeAdapter;
import com.hengxinguotong.zhihuichengjian.bean.Structure;
import com.hengxinguotong.zhihuichengjian.bean.StructureListRes;
import com.hengxinguotong.zhihuichengjian.ui.BaseActivity;
import com.hengxinguotong.zhihuichengjian.ui.engineering.reservation.AddReservationActivity;
import com.hengxinguotong.zhihuichengjian.ui.quality.MeasureActivity;
import com.hengxinguotong.zhihuichengjian.ui.quality.QualityTestActivity;
import com.hengxinguotong.zhihuichengjian.ui.security.DailySecurityTestActivity;
import com.hengxinguotong.zhihuichengjian.ui.security.SecurityAccidentActivity;
import com.hengxinguotong.zhihuichengjian.ui.security.SecurityTestActivity;
import com.hengxinguotong.zhihuichengjian.utils.SPUtil;
import com.hengxinguotong.zhihuichengjian.utils.Utils;
import com.hengxinguotong.zhihuichengjian.widget.font.HXTextView;
import com.lidroid.xutils.http.RequestParams;

import org.apache.http.entity.StringEntity;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.UnsupportedEncodingException;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by liux on 2017/3/27.
 */

public class StructureActivity extends BaseActivity implements AdapterView.OnItemClickListener{
    @BindView(R.id.iv_back)
    HXTextView ivBack;
    @BindView(R.id.save_tv)
    HXTextView saveTv;
    @BindView(R.id.rl_title)
    RelativeLayout rlTitle;
    @BindView(R.id.structure_lv)
    ListView structureLv;
    private StructureTreeAdapter structureTreeAdapter;
    private Structure structure;
    public static  int FROM = 1; //1 创建任务界面  2  任务预约界面 3 质量检查界面 4 质量事故报告 5施工安全检查 6日常安全检查 7实测实量

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_structure);
        ButterKnife.bind(this);
        structure = (Structure) getIntent().getSerializableExtra("structure");
//        saveTv.setVisibility(View.GONE);
        if (structure == null) {
            getStructureInfo(getResources().getString(R.string.loading));
        } else {
            structureTreeAdapter = new StructureTreeAdapter(structure.getData(), StructureActivity.this);
            structureLv.setAdapter(structureTreeAdapter);
            structureLv.setOnItemClickListener(StructureActivity.this);
            if(structure.getData().get(0).getData() == null || structure.getData().get(0).getData().size() == 0){
//                    saveTv.setVisibility(View.VISIBLE);
            }
        }
    }

    @OnClick({R.id.iv_back, R.id.save_tv})
    public void onClick(View view) {
        Intent intent;
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.save_tv:
                if (structureTreeAdapter != null) {
                    switch (StructureActivity.FROM){//1 创建任务界面  2  任务预约界面 3 质量检查界面 4 质量事故报告 3 安全检查界面
                        case 1:
                            intent = new Intent(this, TaskCreateActivity.class);
                            intent.putExtra("struct", structureTreeAdapter.getSelect());
                            startActivity(intent);
                            break;
                        case 2:
                            intent = new Intent(this, AddReservationActivity.class);
                            intent.putExtra("struct", structureTreeAdapter.getSelect());
                            startActivity(intent);
                            break;
                        case 3:
                            intent = new Intent(this, QualityTestActivity.class);
                            intent.putExtra("struct", structureTreeAdapter.getSelect());
                            startActivity(intent);
                            break;
                        case 4:
                            intent = new Intent(this, SecurityAccidentActivity.class);
                            intent.putExtra("struct", structureTreeAdapter.getSelect());
                            startActivity(intent);
                            break;
                        case 5:
                            intent = new Intent(this, SecurityTestActivity.class);
                            intent.putExtra("struct", structureTreeAdapter.getSelect());
                            startActivity(intent);
                            break;
                        case 6:
                            intent = new Intent(this, DailySecurityTestActivity.class);
                            intent.putExtra("struct", structureTreeAdapter.getSelect());
                            startActivity(intent);
                            break;
                        case 7:
                            intent = new Intent(this, MeasureActivity.class);
                            intent.putExtra("struct", structureTreeAdapter.getSelect());
                            startActivity(intent);
                            break;
                    }
                }
                break;
        }
    }


    private void getStructureInfo(String alert) {
        JSONObject jsonObject = new JSONObject();

        try {
            jsonObject.put("constructionId", SPUtil.getString(this,"constructionid"));
        } catch (JSONException e) {
            e.printStackTrace();
        }

        RequestParams params = new RequestParams();
        params.addHeader("Content-type", "application/json");
        try {
            params.setBodyEntity(new StringEntity(jsonObject.toString(), "UTF-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        Utils.requestData(alert, this, "/taskDistribution/queryScheduledPlanList/" + SPUtil.getString(this,"uuid"), params, new Utils.ListenerJson() {
            @Override
            public void onSuccess(String json) {

                if (!Utils.isEmpty(json)) {
                    Gson gson = new Gson();
                    StructureListRes structureListRes = gson.fromJson(json, StructureListRes.class);
                    if(null != structureListRes.getValue() && structureListRes.getValue().size()>0){
                        structureTreeAdapter = new StructureTreeAdapter(structureListRes.getValue(), StructureActivity.this);
                        structureLv.setAdapter(structureTreeAdapter);
                        structureLv.setOnItemClickListener(StructureActivity.this);
                    }else{
                        showEmptyView(structureLv);
                    }

                }
            }

            @Override
            public void onFailure(String json) {
            }
        });
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Structure structure = (Structure) structureTreeAdapter.getItem(position);
        if (structure.getData().size() == 0) {
            structureTreeAdapter.setSelect(position);
        } else {
            Intent intent = new Intent(StructureActivity.this, StructureActivity.class);
            intent.putExtra("structure", structure);
            startActivity(intent);
        }
    }
}
